package com.badibadi.mytools;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import com.badibadi.infos.Results;

import android.content.Context;
import android.os.Message;

/**
 * 上传图片
 * 
 * @author Administrator
 * 
 */
public class UpdateImageThead extends Thread {
	private String cookie, filePath;
	private long file_Lenght;
	private Context context;
	// private int type;
	private boolean flog = true;// 判断是否true详细图片还是， flass是Logo图片

	String url = null;

	public UpdateImageThead(String filePath, Context context, boolean flog) {
		this.filePath = filePath;
		// this.type = type;
		this.context = context;
		this.flog = flog;
//		   ("进入");
	}

	@Override
	public void run() {
		try {
//			   ("开始成功");
			DefaultHttpClient client = new DefaultHttpClient();
			HttpPost hp = new HttpPost(Constants.BadiUrl + "userDataSet");
			// List<NameValuePair> mapParams=new ArrayList<NameValuePair>();
			// mapParams.add(new BasicNameValuePair("type", type+""));
			// hp.setEntity(new UrlEncodedFormEntity(mapParams));

			File file = new File(filePath);

			MultipartEntity entity = new MultipartEntity();

			// 传参
			entity.addPart("headimg", new FileBody(file));
			// entity.addPart("uploadHead", new StringBody("1"));
			// entity.addPart("uploadBack", new StringBody("1"));
			// entity.addPart("type", new StringBody(type + ""));
			file_Lenght = file.length();
//			   ("文件路径是:" + filePath + " " + "文件大小是:"
//					+ file.length());
			hp.setEntity(entity);
			HttpResponse httpResponse = client.execute(hp);
			InputStream in = httpResponse.getEntity().getContent();
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] data = new byte[1024];
			int count = -1;
			while ((count = in.read(data, 0, 1024)) != -1)
				outStream.write(data, 0, count);

			data = null;
			String re = new String(outStream.toByteArray(), "utf-8");
//			   (re);
			Message message = new Message();

			// 返回
			// Results ree = Utils.checkResult_New(context, re, true);
			//    ("isRet=" + ree.isRet());
			//    ("getRetmsg=" + ree.getRetmsg());
			//    ("isMoredata=" + ree.isMoredata());

			// if (ree.isRet() == true) {// 上传成功
			// if (flog == true) {
			// message.what = 0;
			// } else {
			// message.what = 3;
			// }
			//
			// message.obj = ree.getRetmsg();
			// } else {
			// message.what = 1;
			// message.obj = ree.getRetmsg();
			// }
			// 返回回去
			Constants.UpImg_handler.sendMessage(message);
			// HttpEntity httpEntity = httpResponse.getEntity();
			// String content = EntityUtils.toString(httpEntity);
			//    (content);
			// {"base_resp":{"ret":0,"err_msg":"ok"},"location":"bizfile","type":"image","content":"200110366"}
		} catch (ClientProtocolException e) {
			return;
		} catch (IOException e) {
			   
		}
	}

}
